package per.hnvcam.searchext.services.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import per.hnvcam.searchext.data.dao.StatisticDAO;
import per.hnvcam.searchext.data.model.RuleData;
import per.hnvcam.searchext.data.model.Statistic;
import per.hnvcam.searchext.data.model.SystemConfiguration;
import per.hnvcam.searchext.services.ClassificationService;
import per.hnvcam.searchext.services.RuleService;
import per.hnvcam.searchext.services.SpecialQuery;
import per.hnvcam.searchext.services.StatisticService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;

/**
 * Created by IntelliJ IDEA.
 * User: camhoang
 * Date: 3/5/12
 * Time: 9:37 PM
 * To change this template use File | Settings | File Templates.
 */
@Service("StatisticService")
public class StatisticServiceImpl implements StatisticService {

   @Autowired
   @Qualifier("ClassificationService")
   private ClassificationService classificationService;

   @Autowired
   @Qualifier("RuleService")
   private RuleService ruleService;

   @Autowired
   @Qualifier("SpecialQuery")
   private SpecialQuery specialQuery;

   @Autowired
   @Qualifier("StatisticDAO")
   private StatisticDAO statisticDAO;

   @Override
   public void generateStatistic() {
      int count = 0;
      List<RuleData> baseRules = ruleService.getAllBaseRulesData();
      for (RuleData ruleData : baseRules) {
         List<String> classifiedClasses = classificationService.classify(ruleData.getConditions());
         if (classifiedClasses.contains(ruleData.getClassifiedClass())) {
            count++;
         }
      }
      SystemConfiguration systemConfiguration = specialQuery.getSystemConfiguration();
      Statistic stats = new Statistic();
      stats.setClassifiedClassCount(systemConfiguration.getClasses().size());
      stats.setClassifyRuleSetSize(ruleService.getSizeOfClassifyRules(systemConfiguration.isIncludeManualRules()));
      stats.setCreatedDate(new Date());
      stats.setFlooringSize(systemConfiguration.getFlooringSize());
      stats.setGaGenerationMaximum(systemConfiguration.getGenerationMaximum());
      stats.setNumberOfSample(baseRules.size());
      stats.setPercentage((double) count / baseRules.size());
      statisticDAO.save(stats);
   }

   @Override
   public List findAllStatistic() {
      return statisticDAO.findAllStatistic();
   }

   @Override
   public Statistic getLastStatistic() {
      return statisticDAO.getLastStatistic();
   }

   @Override
   public List getRecentStatistic(int count) {
      return statisticDAO.getRecentStatistic(count);
   }

   @Override
   public List convertToGraphData(List historicalStatistic) {
      if (historicalStatistic == null || historicalStatistic.size() == 0) {
         return null;
      }
      List result = new ArrayList();
      int size = historicalStatistic.size();
      String [] createdDates = new String[size];
      int [] classifiedClassCounts = new int[size];
      int [] gaGenerationMaximums = new int[size];
      int [] flooringSizes = new int[size];
      int [] classifyRuleSetSizes = new int[size];
      int [] numberOfSamples = new int[size];
      int [] percentages = new int[size];
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("''MM/dd/yy hh:mm''");

      for (int i = 0; i < size; i++) {
         Statistic statistic = (Statistic) historicalStatistic.get(i);
         createdDates[size - i - 1] = simpleDateFormat.format(statistic.getCreatedDate());
         classifiedClassCounts[size - i - 1] = statistic.getClassifiedClassCount();
         gaGenerationMaximums[size - i - 1] = statistic.getGaGenerationMaximum();
         flooringSizes[size - i - 1] = statistic.getFlooringSize();
         classifyRuleSetSizes[size - i - 1] = statistic.getClassifyRuleSetSize();
         numberOfSamples[size - i - 1] = statistic.getNumberOfSample();
         percentages[size - i - 1] = getPercentage(statistic);
      }

      result.add(createdDates);
      result.add(classifiedClassCounts);
      result.add(gaGenerationMaximums);
      result.add(flooringSizes);
      result.add(classifyRuleSetSizes);
      result.add(numberOfSamples);
      result.add(percentages);

      return result;
   }

   @Override
   public int getPercentage(Statistic stats) {
      return (int) (stats.getPercentage() * 100);
   }

   @Override
   public void save(Statistic statistic) {
      statisticDAO.save(statistic);
   }

   @Override
   public void removeAllStatistic() {
      statisticDAO.removeAllStatistic();
   }
}
